﻿using UnityEngine;
using System.Collections;

public class TouchDrager : MonoBehaviour {

	private bool isPressed = false;
	private Vector3 prePosition;
	private Vector3 moveOffset;

	// Use this for initialization
	void Start () {
	
	}

	void touchBegan(){
		isPressed = true;
		prePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		moveOffset = Vector3.zero;
	}

	void touchMoved(){
		Vector3 nowPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		moveOffset = nowPos - prePosition;
		prePosition = nowPos;
		transform.position += moveOffset;
	}

	void touchEnded(){
		isPressed = false;
	}

	// Update is called once per frame
	void Update () {


		if (Input.GetMouseButtonDown (0)) {
			touchBegan ();
		}

		if (Input.GetMouseButton (0) && isPressed) {
			touchMoved ();
		}

		if (Input.GetMouseButtonUp (0)) {
			touchEnded ();
		}
	}
}
